package com.itgacl.magic4j.libcommon.component.delayQueue.config;

import lombok.extern.slf4j.Slf4j;
import org.rocksdb.Options;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * RocksDB数据库配置
 *
 */
@Slf4j
@Configuration
public class RocksDBConfig {

    @Value("${delayQueueDb.path}")
    private String delayQueueDbPath;

    @Bean("delayDb")
    public RocksDB delayDb(){
        return build(delayQueueDbPath);
    }

    /**
     * 构建Rocks Db
     *
     * @param path 路径
     * @return RocksDb
     */
    private RocksDB build(String path){
        Options options = new Options();
        options.setCreateIfMissing(true);
        File file = new File(path);
        RocksDB db = null;
        try {
            db = RocksDB.open(options, file.getPath());
        } catch (RocksDBException e) {
            log.error(e.getMessage(), e);
        }
        return db;
    }
}
